/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TerrainBrushControl.h"

#include "PQEngine.h"

namespace PQBuilder
{
	TerrainBrushControl::TerrainBrushControl(QWidget* parent)
		: QListWidget(parent)
	{
		this->setViewMode(QListView::IconMode);
		this->setGridSize(QSize(GRID_SIZE_X, GRID_SIZE_Y));
		this->setFlow(QListView::LeftToRight);
		this->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
		this->setAcceptDrops(true);

		loadIcons();

		connect(this,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(OnItemClicked(QListWidgetItem*)));
	}

	void TerrainBrushControl::OnItemClicked(QListWidgetItem* item)
	{
		int index=this->row(item);
		SEngine.selectTerrainBrush(index);
	}

	void TerrainBrushControl::loadIcons()
	{
		this->clear();
		
		int cnt=SEngine.getNumTerrainBrushTexture();
		for(int i=0;i<cnt;i++){

			PQEngine::GPKFile* file=SEngine.getTerrainBrushTextureFile(i);
			if(!file||!file->ok()){
				return;
			}

			file->open();
			int len=file->length();
			char* buf=new char[len];
			file->read(buf,len);
			file->close();


			QByteArray bytes(buf,len);
			QImage image;
			image.loadFromData(bytes);
			QPixmap pixmap=QPixmap::fromImage(image);
			QString name=QString::fromStdString(file->getFileName());
			QListWidgetItem *item=new QListWidgetItem(QIcon(pixmap),name);
			item->setWhatsThis(name);
			item->setToolTip(name);
			this->addItem(item);
			
			delete buf;
		}

		if(cnt>0){
			this->setCurrentItem(this->item(0));
		}
	}

	void TerrainBrushControl::dragEnterEvent(QDragEnterEvent *evt)
	{
		if (evt->mimeData()->hasFormat("text/uri-list")) {//accept local file system's file
			evt->accept();
		}
	}

	void TerrainBrushControl::dropEvent(QDropEvent *evt)
	{
		if (evt->mimeData()->hasUrls()){
			QList<QUrl> urls = evt->mimeData()->urls(); // returns list of QUrl
			int size=urls.size();
			for(int i=0;i<size;i++){
				QString fileName=urls[i].toLocalFile();
				QFileInfo fileInfo(fileName);
				if(fileInfo.isFile()){//only accept image file such as .png .tga
					if(SEngine.addTerrainBrushTextureFromLocalFS(fileInfo.filePath().toStdString())){
						QImage image(fileName);
						QImage::Format format=image.format();
						
						if(format==QImage::Format_Invalid){
							return;
						}
						QPixmap pixmap=QPixmap::fromImage(image);
						
						QListWidgetItem *item=new QListWidgetItem(QIcon(pixmap),fileInfo.fileName());
						item->setToolTip(fileInfo.fileName());
						this->addItem(item);
					}else{
						QMessageBox box;
						box.setText(QObject::tr("Fail to add texture file!More info on http:://gdk3d.com"));
						box.exec();
					}

				}else{
					QMessageBox box;
					box.setText(QObject::tr("Fail to add texture file,must be a texture file.More info on http:://gdk3d.com"));
					box.exec();
				}
			}
		}
	}

	void TerrainBrushControl::dragLeaveEvent(QDragLeaveEvent *evt)
	{
	}

	void TerrainBrushControl::dragMoveEvent(QDragMoveEvent *evt)
	{
	}



}
